package com.htandroid.htandroid.webservice;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.htandroid.htandroid.entity.UffjqgdList;
import com.htandroid.htandroid.entity.UffjqgdList_detail;
import com.htandroid.htandroid.tools.ToolLog;

public class abXMLQgdDjlist {

    /**
     * 解析评论xml信息
     *
     * @param content
     * @return
     */
    public static ArrayList<UffjqgdList> paraseCommentInfors(String content) {
        List<String> list = new ArrayList<String>();
        content = content.replace("UffjqgdList=anyType", "#=#");
        String[] strs = content.split("#=");
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].contains("#")) {
                list.add(strs[i].replace("#", "").trim().replace(" ", ""));
            }
        }
        ArrayList<UffjqgdList> qgdlist = new ArrayList<UffjqgdList>();
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                list.set(i, list.get(i).replace(";};};}", "\"}"));
            }
            list.set(i, list.get(i).replace(";};", "\"}").replace("=", "\":\"").replace(";", "\",\"").replace("{I", "{\"I"));
            try {
                UffjqgdList info = new UffjqgdList();
                JSONObject dataJson = new JSONObject(list.get(i));
                String cmakedate = "";
                if (string2null(dataJson.getString("cmakedate")) == "") {
                    cmakedate = string2null(dataJson.getString("cmakedate"));
                } else {
                    cmakedate = dataJson.getString("cmakedate").substring(0,10)+" "+dataJson.getString("cmakedate").substring(10,dataJson.getString("cmakedate").length());
                }
                info.setCvouchid(string2null(dataJson.getString("cvouchid")));
                info.setCvouchnum(string2null(dataJson.getString("cvouchnum")));
                info.setDdate(string2null(dataJson.getString("ddate")));
                info.setCdepcode(string2null(dataJson.getString("cdepcode")));
                info.setCdepname(string2null(dataJson.getString("cdepname")));
                info.setCqgtype(string2null(dataJson.getString("cqgtype")));
                info.setCmaker(string2null(dataJson.getString("cmaker")));
                info.setCmakedate(string2null(cmakedate));
                info.setCchecker(string2null(dataJson.getString("cchecker")));
                info.setCcheckdate(string2null(dataJson.getString("ccheckdate")));
                info.setCdefine1(string2null(dataJson.getString("cdefine1")));
                info.setCdefine2(string2null(dataJson.getString("cdefine2")));
                info.setCdefine3(string2null(dataJson.getString("cdefine3")));
                info.setCdefine4(string2null(dataJson.getString("cdefine4")));
                info.setID(string2null(dataJson.getString("ID")));
                info.setCprechecker(string2null(dataJson.getString("cprechecker")));
                info.setCprecheckdate(string2null(dataJson.getString("cprecheckdate")));
                info.setCprecheckertemp(string2null(dataJson.getString("cprecheckertemp")));
                info.setCnocheckreason(string2null(dataJson.getString("cnocheckreason")));
                info.setLinename(string2null(dataJson.getString("linename")));
                info.setCbuyer(string2null(dataJson.getString("cbuyer")));
                info.setCpersonname(string2null(dataJson.getString("cpersonname")));
                info.setClinecode(string2null(dataJson.getString("clinecode")));
                info.setIinorder(string2null(dataJson.getString("iinorder")));
                info.setCcheckchar(string2null(dataJson.getString("ccheckchar")));
                info.setResign(string2null(dataJson.getString("resign")));
                info.setCchar(string2null(dataJson.getString("cchar")));
                info.setIamount(string2null(dataJson.getString("iamount")));
                info.setIsqgattachment(string2null(dataJson.getString("isqgattachment")));
                info.setFjsl(string2null(dataJson.getString("fjsl")));
                info.setStepall(string2null(dataJson.getString("stepall")));
                qgdlist.add(info);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return qgdlist;
    }

    //函数作用是把null 转为空
    public static String string2null(String str) {
        String rtn = "";
        if (str == null || str.equals("null") || str.equals("anyType{}")) {
            rtn = "";
        } else {
            rtn = str;
        }
        return rtn;
    }

    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}  
